package web.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.github.mikephil.charting.components.YAxis;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import common.BaseActivity;
import common.Constant;
import guidePage.LoginPage;
import utils.MyLogUtils;
import web.js.CommentInterface;
import personalPage.activity.PersonalPage;
import utils.SPUtils;

/**
 * Created by Administrator on 2017/3/25.
 * 网页
 */

@ContentView(R.layout.college_news_detail)
public class WebPage extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.college_news_back)
    private ImageView college_news_back;
    @ViewInject(R.id.college_news_detail_webView)
    private WebView college_news_detail_webView;
    @ViewInject(R.id.college_news_publish)
    private TextView college_news_publish;
    private CommentInterface jsToJava;

    private String newid = "";
    private String url = "";
    private boolean bindBack;//点击返回是否直接回退

    private static final String TAG = "WebPage";

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case CommentInterface.LOGIN:
                    Intent i = new Intent(WebPage.this, LoginPage.class);
                    WebPage.this.startActivityForResult(i, Constant.LOGIN_REQUEST_CODE);
                    WebPage.this.overridePendingTransition(R.anim.zoomin, 0);
                    break;
                case CommentInterface.PERSONAL_PAGE:
                    Intent i1 = new Intent(WebPage.this, PersonalPage.class);
                    i1.putExtra("type", "app");
                    i1.putExtra("userId", ((String) msg.obj));
                    startActivity(i1);
                    break;
                case CommentInterface.SHOWBTN:
                    boolean showbtn = (boolean) msg.obj;
                    if (showbtn) {
                        college_news_publish.setVisibility(View.VISIBLE);
                    } else {
                        college_news_publish.setVisibility(View.GONE);
                    }
                    break;
                case CommentInterface.BINDBACK:
                    bindBack = (boolean) msg.obj;
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        newid = getIntent().getStringExtra("newsid");
        url = getIntent().getStringExtra("url");
        initView();
        initWebView();
    }


    private void initView() {
        college_news_back.setOnClickListener(this);
        college_news_publish.setOnClickListener(this);
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWebView() {
        WebSettings settings = college_news_detail_webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setBuiltInZoomControls(true);// 设置支持缩放
        settings.setSupportZoom(false);// 不支持缩放
        settings.setUseWideViewPort(false);// 将图片调整到适合webview大小
        settings.setLoadWithOverviewMode(true);// 缩放至屏幕的大小
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);//支持缓存

        college_news_detail_webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return true;
            }

            @Override
            //加载完页面之后的处理
            public void onPageFinished(WebView view, String url) {
                //网页加载完之后，java调用js方法
                super.onPageFinished(view, url);
            }
        });
        SharedPreferences sharedPreferences = SPUtils.getSharedPreferences();
        String user_id = sharedPreferences.getString(Constant.USER_ID, "");
        jsToJava = new CommentInterface(this, handler, college_news_detail_webView, newid);
        if (!TextUtils.isEmpty(newid)) {
            //新闻页
            MyLogUtils.e(TAG, getNewsUrl(user_id));
            college_news_detail_webView.loadUrl(getNewsUrl(user_id));
        } else if (!TextUtils.isEmpty(url)) {
            //积分商城页
            college_news_detail_webView.loadUrl(getMallUrl(user_id));
        }
        college_news_detail_webView.addJavascriptInterface(jsToJava, "androidShare");
    }

    @Override
    public void onBackPressed() {
        if(bindBack){
            //走web的goBack();
            jsToJava.goBack();//要web做的事情
        }else{
            if(college_news_detail_webView.canGoBack()){
                college_news_detail_webView.goBack();
            }else{
                this.finish();
            }
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.college_news_back:
                this.finish();
                break;
            case R.id.college_news_publish:
                jsToJava.publish();
                break;
            default:
                break;
        }
    }

    //访问新闻页
    private String getNewsUrl(String user_id) {
        String url1;
        url1 = url +"&ac_id=" + user_id;
        return url1;
    }

    //访问积分商城
    private String getMallUrl(String user_id) {
        String url;
        url = this.url + "?&ac_id=" + user_id + "&os=Android";
        return url;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_CANCELED) {
            switch (requestCode) {
                case Constant.LOGIN_REQUEST_CODE:
                    jsToJava.loginSuccess(SPUtils.getSharedPreferences().getString(Constant.USER_ID, ""));
                    break;
                default:
                    break;
            }
        }
    }
}
